#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Export Particles                                          #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 10/03/2016                                             #
# Last Modification: 12/03/2018                                             #
# Author...........: focus-em.org                                           #
#                                                                           #
#############################################################################
#
# SORTORDER: 70
#
# DISPLAY: export_anything_doit
# DISPLAY: export_basedir
# DISPLAY: export_ParticleStar_doit
# DISPLAY: export_ParticleStacks_doit
# DISPLAY: export_ParticleStar_subdir
# DISPLAY: export_driftcoraver_subdir
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set export_anything_doit = ""
set export_basedir = ""
set export_ParticleStar_doit = ""
set export_ParticleStacks_doit = ""
set export_ParticleStar_subdir = ""
set export_driftcoraver_subdir = ""
#
set import_rawstack = ""
set import_gainref = ""
set import_defects = ""
set raw_gaincorrectedstack = ""
set movie_stackname = ""
#
#$end_vars
#
set scriptname = export_particles
#
\rm -f LOGS/${scriptname}.results
source ${proc_2dx}/initialize
#
# This memorizes the current merge directory under the variable "olddir":
set olddir = $PWD
#
if ( ${export_anything_doit} != "0" ) then
  #
  echo "<<@progress: 10>>"
  #
  set dirnum = `cat 2dx_merge_dirfile.dat | wc -l`
  echo ": Will work on ${dirnum} image directories."
  set irun = 1
  #
  # This translates the list of directories to work on into one single long line:
  cat 2dx_merge_dirfile.dat | tr "\n" " " > SCRATCH/2dx_merge_dirfile_oneline.dat
  set dirlist = "`cat SCRATCH/2dx_merge_dirfile_oneline.dat`"
  #
  set totalcount = 0
  #
  # This initiates a big "foreach" loop that sets the variable "dirfile" to each of the items in the "dirlist" and runs the contents of the loop with this value:
  foreach dirfile ( ${dirlist} ) 
    #
    cd ${olddir}/../${dirfile}
    #
    set progress = `echo ${irun} ${dirnum} | awk '{ s = 10 + int( 80 * $1 / $2 ) } END { print s }'`
    echo "<<@progress:${progress}>>"
    ${proc_2dx}/linblock "${irun}/${dirnum}: Working on ${dirfile}. Particles found = ${totalcount}."
    #
    set import_rawstack = `grep "set import_rawstack =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set movie_stackname = `grep "set movie_stackname =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    #
    set local_doit = ${export_anything_doit}
    if ( ${local_doit} == "2" ) then
      set local_doit = 1
    endif
    #
    if ( ! -d ${export_basedir} ) then
      ${proc_2dx}/linblock "WARNING: ${export_basedir} not existing. Creating it."
      \mkdir ${export_basedir} 
    endif
    #
    set oriname = `echo ${import_rawstack} | sed 's/\./\_/g' | sed 's/_mrc//g' | sed 's/_dm4//g' | sed 's/_tif//g' `
    #
    # set sub_export_anything_doit = "${1}"
    # set sub_doit = "${2}"
    # set sub_basedir = "${3}"
    # set sub_targetdir = "${4}"
    # set sub_filename = "${5}"
    # set sub_targetname = "${6}"
    # 
    # \rsync -auvP ${sub_filename} ${sub_basedir}/${sub_targetdir}/${sub_targetname}
    # 
    if ( -e movie_aligned_automatch.star ) then
      set linenum = `cat movie_aligned_automatch.star | wc -l | awk '{ s = $1 - 9 } END { print s }'`
      set totalcount = `echo ${totalcount} ${linenum} | awk '{ s = $1 + $2 } END { print s }'`
      echo ":Found ${linenum} particles. Total number of particles is ${totalcount}."
      #
      source ${proc_2dx}/import_export_sub.com ${export_anything_doit} ${export_ParticleStar_doit} ${export_basedir} ${export_ParticleStar_subdir} movie_aligned_automatch.star ${oriname}_pickedparticles.star
    else
      echo "::WARNING: movie_aligned_automatch.star not found."
    endif
    if ( -e ${oriname}_particles_prep.star ) then
      echo ":Found ${oriname}_particles_prep.star"
      #
      source ${proc_2dx}/import_export_sub.com ${export_anything_doit} ${export_ParticleStacks_doit} ${export_basedir} ${export_ParticleStar_subdir} ${oriname}_particles_prep.star ${oriname}_particles_prep.star
    else
      echo "::WARNING: ${oriname}_particles_prep.star not found."
    endif
    if ( -e ${oriname}_particles_prep.mrcs ) then
      echo ":Found ${oriname}_particles_prep.mrcs"
      #
      source ${proc_2dx}/import_export_sub.com ${export_anything_doit} ${export_ParticleStacks_doit} ${export_basedir} ${export_ParticleStar_subdir} ${oriname}_particles_prep.mrcs ${oriname}_particles_prep.mrcs
    else
      echo "::WARNING: ${oriname}_particles_prep.mrcs not found. Trying the non-normalized stack..."

      if ( -e movie_aligned_particles.mrcs ) then
        echo ":Found  movie_aligned_particles.mrcs"
        #
        source ${proc_2dx}/import_export_sub.com ${export_anything_doit} ${export_ParticleStacks_doit} ${export_basedir} ${export_ParticleStar_subdir} movie_aligned_particles.mrcs ${oriname}_particles.mrcs
      else
        echo "::WARNING: ${oriname}_particles.mrcs also not found."
      endif

    endif
    # 
    @ irun += 1
    cd ${olddir}
  end

    cd ${olddir}/../${dirfile}
    if ( -e ${export_basedir}/${export_driftcoraver_subdir} ) then

      cd ${export_basedir}/${export_driftcoraver_subdir}
      \ln -s ../${export_ParticleStar_subdir}/*.star .

    endif

    cd ${olddir}

else
  #
  echo ":Skipping."
  #
endif
#
echo ":: "
echo ":: "
echo ":: Total number of particles is ${totalcount}."
echo ":: "
echo ":: "
#
echo "<<@progress: 100>>"
echo "<<@evaluate>>"
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
##
#
#
#
